/**
****************************************************************************************
 * @Author: luoweiyong fortwater969@163.com
 * @Version: 
 * @Date: 2024-06-29 11:10:29
 * @LastEditors: luoweiyong fortwater969@163.com
 * @LastEditTime: 2024-06-29 11:10:29
 * @FilePath: \code\Bsp\user\ws2812b.c
 * @Description: 
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
****************************************************************************************
*/
#include "stm8s.h"
#include "ws2812b.h"
#include "user_apps.h"

static void ws2812b_send_bit_1(void);
static void ws2812b_send_bit_0(void);

void ws2812b_gpio_init(void)
{
    GPIO_Init(WS2812_PORT, WS2812_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    GPIO_WriteLow(WS2812_PORT, WS2812_PIN);
}

static void ws2812b_send_bit_1(void)
{
    WS2812_PIN_H();
    delay11NOP();
    WS2812_PIN_L();
    delay3NOP();
}

static void ws2812b_send_bit_0(void)
{
    WS2812_PIN_H();
    delay3NOP();
    WS2812_PIN_L();
    delay11NOP();
}

void ws2812b_reset(void)
{
    WS2812_PIN_L();
    DelayMs(1);
}

/*发送一个字节*/
void rgb_write_byte(u8 color_byte)
{
    u8 i = 0;
    for(i = 0; i < 8; i++)
    {
        if(color_byte & 0x80)
        {
            ws2812b_send_bit_1();
        }
        else
        {
            ws2812b_send_bit_0();
        }
        color_byte <<= 1;
    }
}

void rgb_write_data(u8 green, u8 red, u8 blue)
{
    rgb_write_byte(green);
    rgb_write_byte(red);
    rgb_write_byte(blue);
}
